<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API 连接测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .test-container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .test-item {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .success { border-color: #4CAF50; background-color: #f1f8e9; }
        .error { border-color: #f44336; background-color: #ffebee; }
        .info { border-color: #2196F3; background-color: #e3f2fd; }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover { background-color: #45a049; }
        .result {
            margin-top: 10px;
            padding: 10px;
            background-color: #f9f9f9;
            border-radius: 3px;
            font-family: monospace;
            white-space: pre-wrap;
        }
    </style>
</head>
<body>
    <div class="test-container">
        <h1>🔧 API 连接测试工具</h1>
        <p>这个工具可以帮助你测试前端和后端的连接是否正常。</p>
        
        <div class="test-item info">
            <h3>📋 服务状态检查</h3>
            <div id="serviceStatus">检查中...</div>
        </div>

        <div class="test-item">
            <h3>🌐 API Gateway 测试</h3>
            <button onclick="testApiGateway()">测试 API Gateway</button>
            <div id="apiGatewayResult" class="result"></div>
        </div>

        <div class="test-item">
            <h3>📱 短信验证码测试</h3>
            <button onclick="testSendSms()">发送验证码</button>
            <div id="smsResult" class="result"></div>
        </div>

        <div class="test-item">
            <h3>🔐 登录测试</h3>
            <button onclick="testLogin()">测试登录</button>
            <div id="loginResult" class="result"></div>
        </div>

        <div class="test-item">
            <h3>🔄 前端代理测试</h3>
            <button onclick="testProxy()">测试前端代理</button>
            <div id="proxyResult" class="result"></div>
        </div>
    </div>

    <script>
        // 检查服务状态
        function checkServiceStatus() {
            const status = document.getElementById('serviceStatus');
            status.innerHTML = `
                <strong>当前时间:</strong> ${new Date().toLocaleString()}<br>
                <strong>前端地址:</strong> http://localhost:3000<br>
                <strong>API Gateway:</strong> http://127.0.0.1:8888<br>
                <strong>User Service:</strong> http://127.0.0.1:50052 (gRPC)<br>
                <strong>测试手机号:</strong> 15551423998
            `;
        }

        // 测试 API Gateway
        async function testApiGateway() {
            const result = document.getElementById('apiGatewayResult');
            result.textContent = '测试中...';
            
            try {
                const response = await fetch('http://127.0.0.1:8888/swagger/index.html');
                if (response.ok) {
                    result.textContent = '✅ API Gateway 连接成功！\n状态码: ' + response.status;
                    result.parentElement.className = 'test-item success';
                } else {
                    result.textContent = '❌ API Gateway 连接失败\n状态码: ' + response.status;
                    result.parentElement.className = 'test-item error';
                }
            } catch (error) {
                result.textContent = '❌ API Gateway 连接失败\n错误: ' + error.message;
                result.parentElement.className = 'test-item error';
            }
        }

        // 测试短信验证码
        async function testSendSms() {
            const result = document.getElementById('smsResult');
            result.textContent = '发送中...';
            
            try {
                const response = await fetch('http://127.0.0.1:8888/v1/user/send-sms', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        phone: '15551423998'
                    })
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    result.textContent = '✅ 短信验证码发送成功！\n响应: ' + JSON.stringify(data, null, 2);
                    result.parentElement.className = 'test-item success';
                } else {
                    result.textContent = '❌ 短信验证码发送失败\n状态码: ' + response.status + '\n响应: ' + JSON.stringify(data, null, 2);
                    result.parentElement.className = 'test-item error';
                }
            } catch (error) {
                result.textContent = '❌ 短信验证码发送失败\n错误: ' + error.message;
                result.parentElement.className = 'test-item error';
            }
        }

        // 测试登录
        async function testLogin() {
            const result = document.getElementById('loginResult');
            result.textContent = '测试中...';
            
            try {
                const response = await fetch('http://127.0.0.1:8888/v1/user/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        phone: '15551423998',
                        password: '123456'
                    })
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    result.textContent = '✅ 登录成功！\n响应: ' + JSON.stringify(data, null, 2);
                    result.parentElement.className = 'test-item success';
                } else {
                    result.textContent = '⚠️ 登录测试完成（密码错误是正常的）\n状态码: ' + response.status + '\n响应: ' + JSON.stringify(data, null, 2);
                    result.parentElement.className = 'test-item info';
                }
            } catch (error) {
                result.textContent = '❌ 登录测试失败\n错误: ' + error.message;
                result.parentElement.className = 'test-item error';
            }
        }

        // 测试前端代理
        async function testProxy() {
            const result = document.getElementById('proxyResult');
            result.textContent = '测试中...';
            
            try {
                const response = await fetch('/api/user/send-sms', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        phone: '15551423998'
                    })
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    result.textContent = '✅ 前端代理工作正常！\n响应: ' + JSON.stringify(data, null, 2);
                    result.parentElement.className = 'test-item success';
                } else {
                    result.textContent = '❌ 前端代理失败\n状态码: ' + response.status + '\n响应: ' + JSON.stringify(data, null, 2);
                    result.parentElement.className = 'test-item error';
                }
            } catch (error) {
                result.textContent = '❌ 前端代理失败\n错误: ' + error.message + '\n\n可能的原因:\n1. 前端服务未启动\n2. Vite代理配置问题\n3. 后端服务未运行';
                result.parentElement.className = 'test-item error';
            }
        }

        // 页面加载时检查服务状态
        window.onload = function() {
            checkServiceStatus();
        };
    </script>
</body>
</html>
